package activiti

//import base.OpenJs
import grails.converters.JSON
import grails.plugin.springsecurity.annotation.Secured
import org.activiti.engine.repository.Model

@Secured("IS_AUTHENTICATED_FULLY")
class ActModelController {
    def utilsService
    def repositoryService

    def list() {
        def hql = "from ActModel where 1=:one"
        def hmap = [one: 1]
        if (utilsService.validString(params.name)) {
            hql += "  and name like:name"
            hmap << [name: '%' + params.name.toString().trim() + "%"]
        }
        if (utilsService.validString(params.key)) {
            hql += "  and key like:key"
            hmap << [key: '%' + params.key.toString().trim() + "%"]
        }
        if (utilsService.validString(params.status)) {
            hql += "  and status =:status"
            hmap << [status: params.status.toString()]
        }
        if (utilsService.validString(params.typeCode)) {
            hql += "  and typeCode =:typeCode"
            hmap << [typeCode: params.typeCode.toString()]
        }
        if (utilsService.validString(params.typeName)) {
            hql += "  and typeName like:typeName"
            hmap << [typeName: '%' + params.typeName.toString().trim() + "%"]
        }
        hql+=" order by dateCreated, id "
        def result = [success: true, gridroot: [], total: 0], list
        if (utilsService.isNumber(params.offset) && utilsService.isNumber(params.max)) {
            list = ActModel.findAll(hql, hmap, [offset: params.offset as int, max: params.max as int])
        } else list = ActModel.findAll(hql, hmap)
        result.total = ActModel.executeQuery("select  count(*) " + hql, hmap)
        result.gridroot = ActModel.newInstance().buildDomainList(list)
        render result as JSON
    }

    def updateBaseInfo() {
        log.debug(params)
        def record = params
        Model model = repositoryService.getModel(record.id)
        if (utilsService.validString(record.name)) model.setName(record.name)
        if (utilsService.validString(record.key)) model.key = record.key
        if (utilsService.validString(record.version)) model.version = record.version
        repositoryService.saveModel(model)

        //  add ActModel
        ActModel actModel = ActModel.get(model.id)
        if (!actModel) {
            actModel = new ActModel(id: model.id)
            actModel.properties = model.properties
        }
        if (utilsService.validString(record.description)) actModel.description = record.description
        if (utilsService.validString(record.typeCode)) actModel.typeCode = record.typeCode
        if (utilsService.validString(record.typeName)) actModel.typeName = record.typeName
        if (utilsService.validString(record.version)) actModel.version = record.version
        if (utilsService.validString(record.bbms)) actModel.bbms = record.bbms
        if (!actModel.save(flush: true)) {
            actModel.errors.each {
                log.error(it.toString())
            }
        }
        render true
    }
    // 未被业务关联的流程定义列表
    def unsoluModelList() {
        log.debug(params)
        def exihql=" select id from ActSolution where actModel=am "
        if (utilsService.validString(params.actSolutionId)){
            exihql+=" and  actModel.openJs.controlerName!='"+ActSolution.get(params.actSolutionId as long)?.actModel?.openJs?.controlerName+"'"
        }
        def hql = "from ActModel am where 1=:one  and not exists (${exihql})"
        def hmap = [one: 0]
        if (utilsService.validString(params.name)) {
            hql += "  and name like:name"
            hmap << [name: '%' + params.name.toString().trim() + "%"]
            hmap.one = 1
        }
        if (utilsService.validString(params.key)) {
            hql += "  and key like:key"
            hmap << [key: '%' + params.key.toString().trim() + "%"]
            hmap.one = 1
        }
        if (utilsService.validString(params.status)) {
            hql += "  and status =:status"
            hmap << [status: params.status.toString()]
            hmap.one = 1
        }
        if (utilsService.validString(params.typeCode)) {
            hql += "  and typeCode =:typeCode"
            hmap << [typeCode: params.typeCode.toString()]
            hmap.one = 1
        }
        if (utilsService.validString(params.typeName)) {
            hql += "  and typeName like:typeName"
            hmap << [typeName: '%' + params.typeName.toString().trim() + "%"]
            hmap.one = 1
        }
        log.debug(hql+"  "+hmap)
        def result = [success: true, gridroot: [], total: 0], list
        if (utilsService.isNumber(params.offset) && utilsService.isNumber(params.max)) {
            list = ActModel.findAll(hql, hmap, [offset: params.offset as int, max: params.max as int])
        } else list = ActModel.findAll(hql, hmap)
        result.total = ActModel.executeQuery("select  count(*) " + hql, hmap)[0]?:0
        result.gridroot = ActModel.newInstance().buildDomainList(list)
        render result as JSON
    }

    //得到每个环节的 表单 下拉
//    def formCombo() {
//        def hql = "from OpenJs where 1=:one"
//        def hmap = [one: 1]
//        if (params.isStartTask.toString() == '1') {
//            hql += "  and  fqActionUrl is not null "
//        } else {// 不是发起就是审核环节
//            hql += "  and type3='sh' "
//        }
//        if (utilsService.validString(params.typeCode)) {
//            hql += "  and lclx.code=:typeCode"
//            hmap << [typeCode: params.typeCode]
//        }
//        def list = OpenJs.findAll(hql, hmap)
//        def result=[root:[]]
//        list.each {
//            result.root<<[id: it.id,name: it.jsName+" "+it.description,formName:it.jsName,fqActionUrl:it.fqActionUrl]
//        }
//        render result as JSON
//    }
}
